package it.cst.entity.service;

import it.cst.entity.Group;
import it.cst.entity.Match;
import it.cst.entity.Player;
import it.cst.entity.dao.GroupDAO;
import it.cst.util.GroupStat;
import it.cst.util.GroupStatComparator;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service
public class GroupServiceImpl implements GroupService{
	@Autowired
	private GroupDAO groupDAO;
	
	
	public GroupDAO getGroupDAO() {
		return groupDAO;
	}

	public void setGroupDAO(GroupDAO groupDAO) {
		this.groupDAO = groupDAO;
	}

	@Transactional
	public Group addGroup(Group group) {
		Integer id = groupDAO.addGroup(group);
		return groupDAO.getGroup(id);
		
	}

	@Override
	public List<Group> listGroup() {
		// TODO Auto-generated method stub
		return null;
	}

	@Transactional
	public void removeGroup(Group g) {
		groupDAO.removeGroup(g);
	}

	@Transactional
	public Group getGroup(Integer id) {
		return groupDAO.getGroup(id);
	}

	@Transactional
	public List<GroupStat> getGroupStat() {
		List<GroupStat> ret = new ArrayList<GroupStat>();
		HashMap<String,GroupStat> hg = new HashMap<String,GroupStat>();
		List<Group> gl = groupDAO.listGroup(); 
		if(gl != null && !gl.isEmpty()){
			for(Group g : gl)
			{
				//to unique identify group, map key is composed by union of iduser and idrole of each player 
				List<Player> pl = g.getPlayers();
				String key = "";
				for(Player p : pl){
					key = key + p.getIduser();
				}
				if(hg.get(key) == null){
					GroupStat gs = new GroupStat();
					gs.setGroupKey(key);
					Match m = g.getMatch();
					gs.getMatches().add(m);
					for(Player p : pl){
						gs.getUserPlayers().add(p.getUser());
					}
					if(g.getWinner().compareTo(new Integer(1)) == 0){
						gs.setTotMatchWin(1);
					}
					else{
						gs.setTotMatchLoose(1);
					}
					hg.put(key, gs);
				}
				else{
					GroupStat gs = hg.get(key);
					Match m = g.getMatch();
					gs.getMatches().add(m);
					if(g.getWinner().compareTo(new Integer(1)) == 0){
						gs.setTotMatchWin(gs.getTotMatchWin() + 1);
					}
					else{
						gs.setTotMatchLoose(gs.getTotMatchLoose() + 1);
					}
				}
			}
			for(String key : hg.keySet()){
				GroupStat gs = hg.get(key);
				gs.setTotMatch(gs.getTotMatchWin() + gs.getTotMatchLoose());
				gs.setPercMatchWin(new Double(gs.getTotMatchWin()) / gs.getTotMatch() * 100);
				gs.setPercMatchLoose(new Double(gs.getTotMatchLoose()) / gs.getTotMatch() * 100);
				ret.add(hg.get(key));
			}
			//order by : perc win asc, perc loose desc, #match asc
			java.util.Collections.sort(ret, new GroupStatComparator());
		}
		return ret;
	}

	@Transactional
	public List<Group> listGroup(Integer idMatch) {
		return groupDAO.listGroup(idMatch);
	}

}
